package net.lmc.realtime.jtp.dwd.data;

import java.sql.*;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ClickHouseDataInsertion {
    private static final String URL = "jdbc:clickhouse://node103:8123/traffic";
    private static final String USER = "default";
    private static final String PASSWORD = "";
    private static final int BATCH_SIZE = 10000000;

    public static void main(String[] args) {
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            List<WikiStatRecord> records = generateSampleData(BATCH_SIZE);
            insertData(connection, records);
            System.out.printf("成功插入 %d 条数据到 wikistat 表%n", records.size());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static List<WikiStatRecord> generateSampleData(int count) {
        List<WikiStatRecord> records = new ArrayList<>(count);
        Random random = new Random();
        String[] projects = {"wikipedia", "wikimedia", "wikidata", "wikibooks"};
        String[] subprojects = {"main", "article", "category", "special"};

        for (int i = 0; i < count; i++) {
            // 生成过去7天内的随机时间
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime randomTime = now.minus(random.nextInt(168), ChronoUnit.HOURS);
            long timestamp = randomTime.toEpochSecond(ZoneOffset.UTC);

            // 随机项目和子项目（低基数）
            String project = projects[random.nextInt(projects.length)];
            String subproject = subprojects[random.nextInt(subprojects.length)];

            // 随机路径和访问量
            String path = generateRandomPath(random);
            int hits = random.nextInt(10000) + 1;

            records.add(new WikiStatRecord(timestamp, project, subproject, path, hits));
        }
        return records;
    }

    private static String generateRandomPath(Random random) {
        int sections = random.nextInt(4) + 1;
        StringBuilder path = new StringBuilder();
        for (int i = 0; i < sections; i++) {
            path.append('/').append(generateRandomString(random, random.nextInt(6) + 3));
        }
        return path.toString();
    }

    private static String generateRandomString(Random random, int length) {
        String chars = "abcdefghijklmnopqrstuvwxyz";
        StringBuilder result = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            result.append(chars.charAt(random.nextInt(chars.length())));
        }
        return result.toString();
    }

    private static void insertData(Connection connection, List<WikiStatRecord> records) throws SQLException {
        String sql = "INSERT INTO wikistat (time, project, subproject, path, hits) VALUES (?, ?, ?, ?, ?)";
        try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
            for (WikiStatRecord record : records) {
                pstmt.setTimestamp(1, new Timestamp(record.time * 1000));
                pstmt.setString(2, record.project);
                pstmt.setString(3, record.subproject);
                pstmt.setString(4, record.path);
                pstmt.setInt(5, record.hits);
                pstmt.addBatch();
            }
            pstmt.executeBatch();
        }
    }

    static class WikiStatRecord {
        long time;
        String project;
        String subproject;
        String path;
        int hits;

        public WikiStatRecord(long time, String project, String subproject, String path, int hits) {
            this.time = time;
            this.project = project;
            this.subproject = subproject;
            this.path = path;
            this.hits = hits;
        }
    }
}